package controllers

import (
	"PMS/models"
	"github.com/astaxie/beego"
	// "log"
)

type MainController struct {
	beego.Controller
}

func (mainCtr *MainController) Get() {
	mainCtr.Data["Website"] = "beego.me"
	mainCtr.Data["Email"] = "astaxie@gmail.com"
	mainCtr.TplNames = "index.html"
}

// ------------------LoginController
type LoginController struct {
	beego.Controller
}

func (loginCtr *LoginController) Get() {
	loginCtr.Data["username"] = loginCtr.Ctx.Input.Cookie("username")
	loginCtr.Data["password"] = loginCtr.Ctx.Input.Cookie("password")
	loginCtr.Data["remember"] = loginCtr.Ctx.Input.Cookie("remember")

	loginCtr.TplNames = "login.html"
}

func (loginCtr *LoginController) Post() {
	u := &models.User{}
	if err := loginCtr.ParseForm(u); err != nil {
		loginCtr.Ctx.WriteString(err.Error())
	}

	if u.UserLogin() {
		remember := loginCtr.Input().Get("remember") == "on"
		if remember {
			loginCtr.Ctx.SetCookie("username", u.Username, 100)
			loginCtr.Ctx.SetCookie("password", u.Password, 100)
			loginCtr.Ctx.SetCookie("remember", "true", 100)
		} else {
			loginCtr.Ctx.SetCookie("username", "", -1)
			loginCtr.Ctx.SetCookie("password", "", -1)
			loginCtr.Ctx.SetCookie("remember", "", -1)
		}
		loginCtr.Ctx.WriteString("ok")
	} else {
		loginCtr.Redirect("/login", 302)
	}
}
